<template>
  <div class="table-section">
    <div class="table-section_tool">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="billNo">
          <el-input v-model="queryParams.billNo" placeholder="工单号" clearable></el-input>
        </el-form-item>
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #status="{ row }">
        <dict-tag :options="maintenance_bill_status" :value="row.status" />
      </template>
      <template #startFrom="{ row }"> {{row.startFrom}} - {{row.endTo}}</template>
      <template #processDuration="{ row }"> {{getDuration(row.startFrom,row.endTo)}} </template>

      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)">详情</el-button>
        <!-- <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['system:tenant:edit']">详情</el-button> -->
      </template>

      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup name="StationEnergyRecord" lang="ts">
import { listMaintenance } from '@/api/station/maintenance/index';
import { BizStationMaintenanceVo } from '@/api/station/maintenance/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  deviceId: [String, Number]
});
const { maintenance_bill_status } = toRefs<any>(proxy?.useDict('maintenance_bill_status', 'maintenance_bill_type'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  billNo: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  if (!props.deviceId) return;
  try {
    loading.value = true;
    const res = await listMaintenance({ ...queryParams.value, deviceId: props.deviceId, type: 1 }); //类型：1-维修，2-巡检
    loading.value = false;
    data.value = res.rows;
    total.value = res.total;
  } catch (err) {
    console.log(err);
    loading.value = false;
  }
};

const getDuration = (start, to) => {
  let sDate = new Date(start);
  let eDate = new Date(to);

  return (eDate.getTime() - sDate.getTime()) / (1000 * 60 * 60 * 24) + '天';
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 详情按钮操作 */
const handleDetail = async (row?: BizStationMaintenanceVo) => {
  //proxy?.$modal.msg('模块开发中...');
  let type = row.type;

  if (type == '1') proxy?.$tab.openPage('/station/maintenance/maintain/info', '维修工单详情', { id: row.maintenanceId });
  else if (type == '2') proxy?.$tab.openPage('/station/maintenance/patrol/info', '巡检工单详情', { id: row.maintenanceId });
};
onMounted(() => {
  if (props.deviceId) {
    getList();
  }
});
</script>
